{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pondr: Fostering Connection through Good Conversation\n",
    "\n",
    "We tend to chat all the time with friends new and olds, but often it feels like we’re just scratching at the surface, or defaulting to predictable, mundane conversations. Really good conversations — ones that introduce an element of vulnerability, spur a moment of fun, or create a deep sense of closeness — are few and far between. And when we have those types of conversations, we remember them.\n",
    "\n",
    "Pondr is a game that turns strangers into friends, and friends into besties, by fostering connection and closeness through really good conversations. Using Cohere, Pondr generates question prompts on command that are uniquely tailored to the players’ setting. Whether you’re looking to deepen a connection with someone you’ve known forever, or you’re just hoping to become more familiar with a new friend, Pondr will help you drive the right sort of conversation.\n",
    "\n",
    "You can build your own version of Pondr by following these simple steps: \n",
    "\n",
    "1. Generate potential conversation questions\n",
    "2. Rank the generated questions\n",
    "3. Put the generation and classification behind an interface\n",
    "\n",
    "In this notebook we will walk through the first two steps.\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setup\n",
    "Install and import the tools we will need as well as initializing the Cohere model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TODO: upgrade to \"cohere>5\"",
"!pip install \"cohere<5\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cohere\n",
    "from cohere.responses.classify import Example\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "co=cohere.Client('YOUR_API_KEY')"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1) Generate Potential Conversation Questions\n",
    "Generate a list of potential conversation questions and retain the first 10."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#user_input is hardcoded for this example\n",
    "user_input='I am meeting up with a coworker. We are meeting at a fancy restaurant. I wanna ask some interesting questions. These questions should be deep.'\n",
    "prompt=user_input+'\\nHere are 10 interesting questions to ask:\\n1)'\n",
    "response=co.generate(model='xlarge', prompt=prompt, max_tokens=200, temperature=5).generations[0].text\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generation_to_df(generation):\n",
    "    generation=response.split('\\n')\n",
    "    clean_questions=[]\n",
    "    for i in range(10):\n",
    "        curr_q=generation[i]\n",
    "        clean_questions.append(curr_q[curr_q.find(')')+1:])\n",
    "    clean_q_df=pd.DataFrame(clean_questions, columns=['questions'])\n",
    "    return clean_q_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_q_df = generation_to_df(response)\n",
    "pd.options.display.max_colwidth=150\n",
    "clean_q_df"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) Classify Questions\n",
    "Rank and sort the questions based on interestingness and specificity."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# example labelled questions that act as a training samples for the classification\n",
    "interestingness=[\n",
    "    Example(\"What do you think is the hardest part of what I do for a living?\", \"Not Interesting\"), \n",
    "    Example(\"What\\'s the first thing you noticed about me?\", \"Interesting\"), \n",
    "    Example(\"Do you think plants thrive or die in my care?\", \"Interesting\"), \n",
    "    Example(\"Do I seem like more of a creative or analytical type?\", \"Interesting\"), \n",
    "    Example(\"What subject do you think I thrived at in school?\", \"Not Interesting\"), \n",
    "    Example(\"What\\'s been your happiest memory this past year?\", \"Interesting\"), \n",
    "    Example(\"What lesson took you the longest to un-learn?\", \"Not Interesting\"), \n",
    "    Example(\"How can you become a better person?\", \"Not Interesting\"), \n",
    "    Example(\"Do you think I intimidate others? Why or why not?\", \"Interesting\"), \n",
    "    Example(\"What\\'s the most embarrassing thing that happened to you on a date?\", \"Not Interesting\"), \n",
    "    Example(\"How would you describe what you think my type is in three words?\", \"Interesting\"), \n",
    "    Example(\"What do you think I\\'m most likely to splurge on?\", \"Interesting\"), \n",
    "    Example(\"As a child what do you think I wanted to be when I grow up?\", \"Interesting\"), \n",
    "    Example(\"Do you think you are usually early, on time, or late to events?\", \"Not Interesting\"), \n",
    "    Example(\"Do you think I was popular at school?\", \"Interesting\"), \n",
    "    Example(\"What questions are you trying to answer most in your life right now?\", \"Not Interesting\")]\n",
    "specificity=[\n",
    "    Example(\"What\\'s the first thing you noticed about me?\", \"Specific\"), \n",
    "    Example(\"Do you think plants thrive or die in my care?\", \"Specific\"), \n",
    "    Example(\"Do I seem like more of a creative or analytical type?\", \"Not Specific\"), \n",
    "    Example(\"How would you describe what you think my type is in three words?\", \"Not Specific\"), \n",
    "    Example(\"What do you think I\\'m most likely to splurge on?\", \"Specific\"), \n",
    "    Example(\"What subject do you think I thrived at in school?\", \"Not Specific\"), \n",
    "    Example(\"As a child what do you think I wanted to be when I grow up?\", \"Specific\"), \n",
    "    Example(\"Do you think I was popular at school?\", \"Specific\"), \n",
    "    Example(\"Do you think you\\'re usually early, on time, or late to events?\", \"Specific\"), \n",
    "    Example(\"Do you think I intimidate others? Why or why not?\", \"Specific\"), \n",
    "    Example(\"What\\'s been your happiest memory this past year?\", \"Not Specific\"), \n",
    "    Example(\"What subject do you think I thrived at in school?\", \"Specific\"), \n",
    "    Example(\"What\\'s the biggest mistake that you think you needed to make to become who you are now?\", \"Specific\"), \n",
    "    Example(\"Is there anything you\\'ve done recently that you\\'re incredibly proud of?\", \"Not Specific\"), \n",
    "    Example(\"How are you and your siblings similar?\", \"Not Specific\"), \n",
    "    Example(\"What\\'s the worst pain you have ever been in that wasn\\'t physical?\", \"Specific\"), \n",
    "    Example(\"Has a stranger ever changed your life?\", \"Specific\"), \n",
    "    Example(\"Do you think the image you have of yourself matches the image other people see you as?\", \"Specific\"), \n",
    "    Example(\"What would your younger self not believe about your life today?\", \"Specific\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_attribute(df, attribute, name, target):\n",
    "\n",
    "  response = co.classify(\n",
    "    model='medium',\n",
    "    inputs=list(df['questions']),\n",
    "    examples=attribute)\n",
    "\n",
    "  q_conf=[]\n",
    "  for q in response.classifications:\n",
    "    q_conf.append(q.labels[target].confidence)\n",
    "\n",
    "  df[name]=q_conf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "add_attribute(clean_q_df, interestingness, 'interestingness', 'Interesting')\n",
    "add_attribute(clean_q_df, specificity, 'specificity', 'Specific')\n",
    "clean_q_df['average']= clean_q_df.iloc[:,1:].mean(axis=1)\n",
    "clean_q_df.sort_values(by='average', ascending=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.13 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "4f64e0feeb48255507f9272d9b90b042729172fc814dfe10c82ed9c24fde65d7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
